package com.agile.major.web.rest;

import com.agile.major.domain.TraningDataHistory;
import com.agile.major.service.TraningDataHistoryService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TraningDataHistory.
 */
@RestController
@RequestMapping("/api")
public class TraningDataHistoryResource {

    private final Logger log = LoggerFactory.getLogger(TraningDataHistoryResource.class);

    private static final String ENTITY_NAME = "traningDataHistory";

    private final TraningDataHistoryService traningDataHistoryService;

    public TraningDataHistoryResource(TraningDataHistoryService traningDataHistoryService) {
        this.traningDataHistoryService = traningDataHistoryService;
    }

    /**
     * POST  /traning-data-histories : Create a new traningDataHistory.
     *
     * @param traningDataHistory the traningDataHistory to create
     * @return the ResponseEntity with status 201 (Created) and with body the new traningDataHistory, or with status 400 (Bad Request) if the traningDataHistory has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/traning-data-histories")
    @Timed
    public ResponseEntity<TraningDataHistory> createTraningDataHistory(@RequestBody TraningDataHistory traningDataHistory) throws URISyntaxException {
        log.debug("REST request to save TraningDataHistory : {}", traningDataHistory);
        if (traningDataHistory.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new traningDataHistory cannot already have an ID")).body(null);
        }
        TraningDataHistory result = traningDataHistoryService.save(traningDataHistory);
        return ResponseEntity.created(new URI("/api/traning-data-histories/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /traning-data-histories : Updates an existing traningDataHistory.
     *
     * @param traningDataHistory the traningDataHistory to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated traningDataHistory,
     * or with status 400 (Bad Request) if the traningDataHistory is not valid,
     * or with status 500 (Internal Server Error) if the traningDataHistory couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/traning-data-histories")
    @Timed
    public ResponseEntity<TraningDataHistory> updateTraningDataHistory(@RequestBody TraningDataHistory traningDataHistory) throws URISyntaxException {
        log.debug("REST request to update TraningDataHistory : {}", traningDataHistory);
        if (traningDataHistory.getId() == null) {
            return createTraningDataHistory(traningDataHistory);
        }
        TraningDataHistory result = traningDataHistoryService.save(traningDataHistory);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, traningDataHistory.getId().toString()))
            .body(result);
    }

    /**
     * GET  /traning-data-histories : get all the traningDataHistories.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of traningDataHistories in body
     */
    @GetMapping("/traning-data-histories")
    @Timed
    public ResponseEntity<List<TraningDataHistory>> getAllTraningDataHistories(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of TraningDataHistories");
        Page<TraningDataHistory> page = traningDataHistoryService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/traning-data-histories");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


    @GetMapping("/traning-data-histories/traning/{traningId}")
    @Timed
    public ResponseEntity<Page<TraningDataHistory>> getAllTraningDataHistories(@PathVariable Long traningId, @ApiParam Pageable pageable) {
        log.debug("REST request to get a page of TraningDataHistories");
        Page<TraningDataHistory> page = traningDataHistoryService.findByTraning(traningId,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/traning-data-histories/traning/{traningId}");
        return new ResponseEntity<>(page,headers,HttpStatus.OK);

    }

    /**
     * GET  /traning-data-histories/:id : get the "id" traningDataHistory.
     *
     * @param id the id of the traningDataHistory to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the traningDataHistory, or with status 404 (Not Found)
     */
    @GetMapping("/traning-data-histories/{id}")
    @Timed
    public ResponseEntity<TraningDataHistory> getTraningDataHistory(@PathVariable Long id) {
        log.debug("REST request to get TraningDataHistory : {}", id);
        TraningDataHistory traningDataHistory = traningDataHistoryService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(traningDataHistory));
    }

    /**
     * DELETE  /traning-data-histories/:id : delete the "id" traningDataHistory.
     *
     * @param id the id of the traningDataHistory to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/traning-data-histories/{id}")
    @Timed
    public ResponseEntity<Void> deleteTraningDataHistory(@PathVariable Long id) {
        log.debug("REST request to delete TraningDataHistory : {}", id);
        traningDataHistoryService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
